package my.edu.model.bills.sales.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import my.edu.common.constant.MessageConstant;
import my.edu.common.exception.Order.SalesException;
import my.edu.model.bills.sales.entity.SalesOrder;
import my.edu.model.bills.sales.entity.SalesReturnOrder;
import my.edu.model.bills.sales.entity.dto.SalesOrderSearchDTO;
import my.edu.model.bills.sales.entity.dto.SalesReturnDetailDTO;
import org.springframework.stereotype.Service;
import my.edu.model.bills.sales.entity.SalesReturnOrderDetail;
import my.edu.model.bills.sales.mapper.SalesReturnOrderDetailMapper;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.List;

@Service
public class SalesReturnOrderDetailServiceImpl extends ServiceImpl<SalesReturnOrderDetailMapper, SalesReturnOrderDetail> implements ISalesReturnOrderDetailService{

    @Override
    @Transactional
    public void saveBatchByDTO(List<SalesReturnDetailDTO> salesReturnDetailDTOList, Long id) {
        List<SalesReturnOrderDetail> purchaseOrderDetails = salesReturnDetailDTOList.stream().map(i -> {
            SalesReturnOrderDetail detail = BeanUtil.copyProperties(i, SalesReturnOrderDetail.class);
            detail.setSalesReturnOrderId(id);
            return detail;
        }).toList();
        saveBatch(purchaseOrderDetails);
    }

    @Override
    @Transactional
    public void updateBatchByDTO(List<SalesReturnDetailDTO> salesReturnDetailDTOList, Long id) {
        remove(Wrappers.<SalesReturnOrderDetail>lambdaQuery().eq(SalesReturnOrderDetail::getSalesReturnOrderId,id));
        saveBatchByDTO(salesReturnDetailDTOList,id);
    }

    @Override
    public List<SalesReturnOrderDetail> getReportList(SalesOrderSearchDTO salesOrderSearchDTO) {
        MPJLambdaWrapper<SalesReturnOrderDetail> wrapper = new MPJLambdaWrapper<SalesReturnOrderDetail>()
                .selectAll(SalesReturnOrderDetail.class)
                .leftJoin(SalesReturnOrder.class, SalesReturnOrder::getId, SalesReturnOrderDetail::getSalesReturnOrderId)
                .between(salesOrderSearchDTO.getBeginTime() != null,
                        SalesReturnOrder::getOrderDate, salesOrderSearchDTO.getBeginTime(), salesOrderSearchDTO.getEndTime())
                .eq(salesOrderSearchDTO.getCustomerId()!= null,
                        SalesReturnOrder::getCustomerId, salesOrderSearchDTO.getCustomerId())
                .and(StringUtils.hasText(salesOrderSearchDTO.getMaterialParam()), i->i
                        .like(SalesReturnOrderDetail::getProductNo, salesOrderSearchDTO.getMaterialParam())
                        .or()
                        .like(SalesReturnOrderDetail::getProductName, salesOrderSearchDTO.getMaterialParam()));
        List<SalesReturnOrderDetail> salesOrderDetails = baseMapper.selectJoinList(wrapper);
        return salesOrderDetails;
    }
}
